﻿using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace TreeViewTest
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        public ObservableCollection<Person> Persons { get; set; } =new ObservableCollection<Person>();
        public ObservableCollection<Person> PeopleTree { get; set; }=new ObservableCollection<Person>();

        public MainWindow()
        {
            this.InitializeComponent();

            var p = new Person { Name = "Root" };
            PeopleTree.Add(p);
            
            Persons.Add(new Person { Name = "A" });
            Persons.Add(new Person { Name = "B" });
            Persons.Add(new Person { Name = "C" });
        }

        private void TreeViewItem_DragOver(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
        }

        private async void TextBlock_Drop(object sender, DragEventArgs e)
        {
            var person = (sender as TextBlock).DataContext as Person;
            var data = await e.DataView.GetTextAsync();
            person.Children.Add(new Person {  Name = data });
        }

        private void TextBlock_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            args.Data.SetText((sender as TextBlock).Text);
        }
    }
}
